﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBBase;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Security;
using System.Security.Cryptography;
namespace Business
{
    public partial class Security_activation : Form
    {
        public MainForm m_mainform;

        public static string sip;
        public static string zhanghao;

        public static string xzip;
        public static string xzzhanghao;
        public Security_activation(string szhanghao, string ip)
        {
            InitializeComponent();
            sip = ip;
            zhanghao = szhanghao;
           
            inilist(list, "USERE");
            inicbxzd(list_zd, "zongduan");

            x = this.Width;
            y = this.Height;
            setTag(this);

        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            return cip;
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
       
        void inilist(CCWin.SkinControl.SkinDataGridView cbx, string dataname)
        {
            cbx.Rows.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} order by YHLB", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sxg = "";
                if (dt.Rows[i]["XGJLBS"].ToString().Trim().Contains("是"))
                {
                    sxg = "*";
                }
                else
                {
                    sxg = "";
                }
                cbx.Rows.Add();
                cbx.Rows[i].Cells[0].Value = Decrypt(dt.Rows[i]["ZHMJM"].ToString().Trim()) + sxg;
                if (sxg.Contains("*"))
                {
                    cbx.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                }
                cbx.Rows[i].Cells[1].Value = string.Format("{0}", dt.Rows[i]["JSLX"].ToString().Trim());
                cbx.Rows[i].Cells[2].Value = dt.Rows[i]["ZT"].ToString().Trim();

            }
           

        }
        void inicbxzd(CCWin.SkinControl.SkinDataGridView cbx, string dataname)
        {
            cbx.Rows.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} order by ZD_ID", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbx.Rows.Add();
                cbx.Rows[i].Cells[0].Value = dt.Rows[i]["ZD_IP"].ToString().Trim();
                cbx.Rows[i].Cells[1].Value =dt.Rows[i]["ZD_ZT"].ToString().Trim();

            }


        }


     

       
        private void lstUser_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       

   

        private void Security_activation_Resize(object sender, EventArgs e)
        {
            if (this.Width < 1)
            {
                this.Width = 1024;
                this.Height = 700;
            }
            float newx = (this.Width) / x;
            float newy = (700) / y;
            setControls(newx, newy, this);
        }

        private void but_jhyh_Click(object sender, EventArgs e)
        {
            string ssql = string.Format(@"update  USERE set ZT='{0}' where YHLB='{1}'", "在用", xzzhanghao);
            Query objquery = new Query();
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("用户激活失败");
            }
            else
            {
                MessageBox.Show("用户激活成功");
            }

            inilist(list, "USERE");

            //保存操作日记
            m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "激活用户");

            //判断是否核心功能
            if (m_mainform.shxgn.Contains("激活用户"))
            {
                m_mainform.baocunaqrj("激活用户", "更新", "系统管理", "是", "否", "成功", string.Format("激活用户{0}:{1}", xzzhanghao, ssql));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

               // m_mainform.baocunhxrj("激活用户", string.Format("激活用户{0}:{1}", xzzhanghao, ssql), "更新");// 核心功能日记  日记内容
            }
            else
            {
                m_mainform.baocunaqrj("激活用户", "更新", "系统管理", "否", "否", "成功", string.Format("激活用户{0}:{1}", xzzhanghao, ssql));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

            }
        }

        private void but_yhjs_Click(object sender, EventArgs e)
        {
            string ssql = string.Format(@"update  USERE set ZT='{0}' where YHLB='{1}'", "在用", xzzhanghao);
            Query objquery = new Query();
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("用户解锁失败");
            }
            else
            {
                MessageBox.Show("用户解锁成功");
            }
          //  baocunaqrj(zhanghao, "用户解锁", string.Format(@"{0}解锁锁定的{1}用户", zhanghao, xzzhanghao));
            inilist(list, "USERE");
            //保存操作日记
            m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "用户解锁");
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("解锁用户"))
            {
                m_mainform.baocunhxrj("解锁用户", string.Format("解锁用户{0}:{1}", xzzhanghao, ssql), "更新");// 核心功能日记  日记内容
            }
            else
            {
                m_mainform.baocunaqrj("解锁用户", "更新", "系统管理", "否", "否", "成功", string.Format("解锁用户{0}:{1}", xzzhanghao, ssql));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

            }
        }

        private void but_yhzx_Click(object sender, EventArgs e)
        {
            string ssql = string.Format(@"update  USERE set ZT='{0}' where YHLB='{1}'", "注销", xzzhanghao);
            Query objquery = new Query();
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("用户注销失败");
            }
            else
            {
                MessageBox.Show("用户注销成功");
            }
         //   baocunaqrj(zhanghao, "用户注销", string.Format(@"{0}注销{1}用户", zhanghao, xzzhanghao));
            inilist(list, "USERE");
            //保存操作日记
            m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "用户注销");

            //判断是否核心功能
            if (m_mainform.shxgn.Contains("用户注销"))
            {
                m_mainform.baocunhxrj("用户注销", string.Format("注销用户{0}:{1}", xzzhanghao, ssql), "更新");// 核心功能日记  日记内容
            }
            else
            {
                m_mainform.baocunaqrj("用户注销", "更新", "系统管理", "否", "否", "成功", string.Format("注销用户{0}:{1}", xzzhanghao, ssql));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

            }
        }

        private void but_zdjs_Click(object sender, EventArgs e)
        {
            string ssql = string.Format(@"update  zongduan set ZD_ZT='{0}' where ZD_IP='{1}'", "在用", xzip);
            Query objquery = new Query();
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("终端解锁失败");
            }
            else
            {
                MessageBox.Show("终端解锁成功");
            }
          //  baocunaqrj(zhanghao, "终端解锁", string.Format(@"{0}解锁终端{1}", zhanghao, xzip));
            //保存操作日记
            m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "终端解锁");
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("终端解锁"))
            {
                m_mainform.baocunhxrj("终端解锁", string.Format("终端解锁{0}:{1}", xzip, ssql), "更新");// 核心功能日记  日记内容
            }
            else
            {
                m_mainform.baocunaqrj("终端解锁", "更新", "系统管理", "否", "否", "成功", string.Format("终端解锁{0}:{1}", xzip, ssql));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

            }

            inicbxzd(list_zd, "zongduan");
        }

        private void Security_activation_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("激活用户"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_jhyh.Enabled = false;
                }
            }

            if (m_mainform.sfjy.Contains("解锁用户"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_yhjs.Enabled = false;
                }
            }

            if (m_mainform.sfjy.Contains("用户注销"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_yhzx.Enabled = false;
                }
            }

            if (m_mainform.sfjy.Contains("终端解锁"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_zdjs.Enabled = false;
                }
            }

        }

        private void list_zd_Click(object sender, EventArgs e)
        {
            try
            {
                xzip = this.list.CurrentRow.Cells[0].Value.ToString();
            }
            catch
            { }
        }

        private void list_Click(object sender, EventArgs e)
        {
            try
            {
                xzzhanghao = this.list.CurrentRow.Cells[0].Value.ToString();
            }
            catch
            { 
            }
        }
    }
}
